package win.smartown.android.library.rain;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.FrameLayout;

/**
 * Created by Smartown on 2017/7/28.
 */
public class Sky extends FrameLayout {

    private boolean playing = false;
    private int count = 0;
    private Handler handler = new Handler();

    private float fromSize;
    private float toSize;
    private int totalCount;
    private int interval;
    private int duration;
    private float angle;
    private Drawable unitDrawable;

    public Sky(Context context) {
        super(context);
        init(null);
    }

    public Sky(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public Sky(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public Sky(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        if (attrs == null) {
            fromSize = 0.2f;
            toSize = 0.1f;
            totalCount = 0;
            interval = 50;
            duration = 1000;
            angle = 0;
        } else {
            TypedArray typedArray = getResources().obtainAttributes(attrs, R.styleable.Sky);
            fromSize = typedArray.getFloat(R.styleable.Sky_fromSize, 0.2f);
            toSize = typedArray.getFloat(R.styleable.Sky_toSize, 0.1f);
            totalCount = typedArray.getInt(R.styleable.Sky_totalCount, 0);
            interval = typedArray.getInt(R.styleable.Sky_interval, 50);
            duration = typedArray.getInt(R.styleable.Sky_duration, 1000);
            angle = typedArray.getFloat(R.styleable.Sky_angle, 0);
            unitDrawable = typedArray.getDrawable(R.styleable.Sky_unitDrawable);
            typedArray.recycle();
        }
        if (unitDrawable == null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                unitDrawable = getResources().getDrawable(R.drawable.ic_opacity_black_24dp, getResources().newTheme());
            } else {
                unitDrawable = getResources().getDrawable(R.drawable.ic_opacity_black_24dp);
            }
        }
        if (totalCount == -1) {
            totalCount = Integer.MAX_VALUE;
        }
    }

    public void start() {
        if (playing) {
            return;
        }
        playing = true;
        addRain();
    }

    private void addRain() {
        if (count >= totalCount) {
            return;
        }
        count++;
        int startSize = (int) (getWidth() * fromSize);
        int deltaX = (int) ((getWidth() + startSize) * Math.tan(angle * Math.PI / 180) + (toSize - fromSize) * getWidth() / 2);
        int deltaY = getHeight() + startSize;

        Rain rain = new Rain(getContext(), count, deltaX, deltaY, duration, toSize / fromSize, new Rain.Callback() {
            @Override
            public void onAnimFinished(Rain view, int index) {
                removeView(view);
                if (index == totalCount) {
                    count = 0;
                    playing = false;
                }
            }
        });
        rain.setLayoutParams(new ViewGroup.LayoutParams(startSize, startSize));
        rain.setImageDrawable(unitDrawable);
        rain.setTranslationX((int) (Math.random() * getWidth() * 2));
        rain.setTranslationY(-startSize);
        addView(rain);
        rain.startAnimation();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                addRain();
            }
        }, interval);
    }

}
